<template>
  <div class="page-container">
    <!-- 顶部标题栏 -->
    <div class="header">
      <div class="back-btn" @click="goBack">
        <Left />
      </div>
      <h1 class="title">订单详情</h1>
      <div class="placeholder"></div>
    </div>

    <!-- 订单状态 -->
    <div class="status-section">
      <div class="status-info">
        <div class="status-icon">
          <img :src="getStatusIcon(order.status)" :alt="getStatusText(order.status)" />
        </div>
        <div class="status-details">
          <h3 class="status-text">{{ getStatusText(order.status) }}</h3>
          <p class="status-desc">{{ getStatusDesc(order.status) }}</p>
        </div>
      </div>
    </div>

    <!-- 收货地址 -->
    <div class="address-section">
      <div class="section-title">收货地址</div>
      <div class="address-info">
        <div class="contact-info">
          <span class="name">{{ order.address.name }}</span>
          <span class="phone">{{ order.address.phone }}</span>
        </div>
        <div class="address-detail">
          {{ order.address.province }} {{ order.address.city }} {{ order.address.district }} {{ order.address.address }}
        </div>
      </div>
    </div>

    <!-- 商品信息 -->
    <div class="products-section">
      <div class="section-title">商品信息</div>
      <div class="product-list">
        <div 
          class="product-item" 
          v-for="product in order.products" 
          :key="product.id"
          @click="goToProduct(product.id)"
        >
          <div class="product-image">
            <img :src="product.image" :alt="product.name" />
          </div>
          <div class="product-info">
            <h4 class="product-name">{{ product.name }}</h4>
            <div class="product-spec" v-if="product.spec">{{ product.spec }}</div>
            <div class="product-price">
              <span class="price">¥{{ product.price }}</span>
              <span class="quantity">x{{ product.quantity }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 订单信息 -->
    <div class="order-info-section">
      <div class="section-title">订单信息</div>
      <div class="info-list">
        <div class="info-item">
          <span class="info-label">订单号</span>
          <span class="info-value">{{ order.orderNumber }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">下单时间</span>
          <span class="info-value">{{ order.createTime }}</span>
        </div>
        <div class="info-item" v-if="order.payTime">
          <span class="info-label">付款时间</span>
          <span class="info-value">{{ order.payTime }}</span>
        </div>
        <div class="info-item" v-if="order.shipTime">
          <span class="info-label">发货时间</span>
          <span class="info-value">{{ order.shipTime }}</span>
        </div>
        <div class="info-item" v-if="order.completeTime">
          <span class="info-label">完成时间</span>
          <span class="info-value">{{ order.completeTime }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">支付方式</span>
          <span class="info-value">{{ getPaymentText(order.paymentMethod) }}</span>
        </div>
        <div class="info-item" v-if="order.remark">
          <span class="info-label">订单备注</span>
          <span class="info-value">{{ order.remark }}</span>
        </div>
      </div>
    </div>

    <!-- 费用明细 -->
    <div class="cost-section">
      <div class="section-title">费用明细</div>
      <div class="cost-list">
        <div class="cost-item">
          <span class="cost-label">商品总价</span>
          <span class="cost-value">¥{{ getTotalPrice(order.products).toFixed(2) }}</span>
        </div>
        <div class="cost-item">
          <span class="cost-label">运费</span>
          <span class="cost-value">{{ order.deliveryFee > 0 ? `¥${order.deliveryFee}` : '免费' }}</span>
        </div>
        <div class="cost-item" v-if="order.couponDiscount > 0">
          <span class="cost-label">优惠券</span>
          <span class="cost-value">-¥{{ order.couponDiscount }}</span>
        </div>
        <div class="cost-item total">
          <span class="cost-label">实付金额</span>
          <span class="cost-value">¥{{ order.totalAmount.toFixed(2) }}</span>
        </div>
      </div>
    </div>

    <!-- 物流信息 -->
    <div class="logistics-section" v-if="order.logistics">
      <div class="section-title">物流信息</div>
      <div class="logistics-info">
        <div class="logistics-company">
          <span class="label">快递公司：</span>
          <span class="value">{{ order.logistics.company }}</span>
        </div>
        <div class="logistics-number">
          <span class="label">快递单号：</span>
          <span class="value">{{ order.logistics.trackingNumber }}</span>
        </div>
        <div class="logistics-timeline">
          <div 
            class="timeline-item" 
            v-for="(item, index) in order.logistics.timeline" 
            :key="index"
            :class="{ active: index === 0 }"
          >
            <div class="timeline-dot"></div>
            <div class="timeline-content">
              <div class="timeline-time">{{ item.time }}</div>
              <div class="timeline-desc">{{ item.desc }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部操作栏 -->
    <div class="bottom-actions" v-if="order.status !== 'cancelled'">
      <div class="action-buttons">
        <nut-button 
          v-if="order.status === 'pending'" 
          type="primary" 
          size="large"
          @click="payOrder"
        >
          立即付款
        </nut-button>
        
        <nut-button 
          v-if="order.status === 'shipped'" 
          type="primary" 
          size="large"
          @click="confirmReceive"
        >
          确认收货
        </nut-button>
        
        <nut-button 
          v-if="order.status === 'completed'" 
          type="primary" 
          size="large"
          @click="reviewOrder"
        >
          评价订单
        </nut-button>
        
        <nut-button 
          v-if="order.status === 'pending'" 
          type="default" 
          size="large"
          @click="cancelOrder"
        >
          取消订单
        </nut-button>
        
        <nut-button 
          v-if="order.status === 'completed'" 
          type="default" 
          size="large"
          @click="rebuyOrder"
        >
          再次购买
        </nut-button>
        
        <nut-button 
          v-if="order.status === 'shipped'" 
          type="default" 
          size="large"
          @click="viewLogistics"
        >
          查看物流
        </nut-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Left } from '@nutui/icons-vue'

const route = useRoute()
const router = useRouter()

// 订单数据
const order = ref({
  id: 1,
  orderNumber: 'EN202401150001',
  createTime: '2024-01-15 14:30:25',
  status: 'shipped',
  products: [
    {
      id: 1,
      name: 'iPhone 15 Pro Max 256GB',
      spec: '深空黑色 256GB',
      price: 9999,
      quantity: 1,
      image: 'https://picsum.photos/80/80?random=1'
    }
  ],
  address: {
    name: '张三',
    phone: '13800138000',
    province: '北京市',
    city: '北京市',
    district: '朝阳区',
    address: '三里屯SOHO 1号楼 1001室'
  },
  paymentMethod: 'wechat',
  deliveryFee: 10,
  couponDiscount: 0,
  totalAmount: 10009,
  remark: '请尽快发货',
  payTime: '2024-01-15 14:35:12',
  shipTime: '2024-01-16 10:20:30',
  logistics: {
    company: '顺丰速运',
    trackingNumber: 'SF1234567890',
    timeline: [
      {
        time: '2024-01-16 10:20:30',
        desc: '快件已发出，正在运输中'
      },
      {
        time: '2024-01-16 15:30:45',
        desc: '快件已到达【北京朝阳区三里屯营业点】'
      },
      {
        time: '2024-01-16 16:45:12',
        desc: '快件正在派送中，请保持电话畅通'
      }
    ]
  }
})

// 获取状态图标
const getStatusIcon = (status) => {
  const iconMap = {
    pending: 'https://picsum.photos/60/60?random=1',
    processing: 'https://picsum.photos/60/60?random=1',
    shipped: 'https://picsum.photos/60/60?random=1',
    completed: 'https://picsum.photos/60/60?random=1',
    cancelled: 'https://picsum.photos/60/60?random=1'
  }
  return iconMap[status] || iconMap.pending
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    pending: '待付款',
    processing: '待发货',
    shipped: '待收货',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statusMap[status] || '未知状态'
}

// 获取状态描述
const getStatusDesc = (status) => {
  const descMap = {
    pending: '请尽快完成付款，超时订单将自动取消',
    processing: '商家正在处理您的订单，请耐心等待',
    shipped: '商品正在配送中，请注意查收',
    completed: '订单已完成，感谢您的购买',
    cancelled: '订单已取消'
  }
  return descMap[status] || ''
}

// 获取支付方式文本
const getPaymentText = (method) => {
  const methodMap = {
    wechat: '微信支付',
    alipay: '支付宝',
    card: '银行卡'
  }
  return methodMap[method] || '未知方式'
}

// 获取商品总价格
const getTotalPrice = (products) => {
  return products.reduce((total, product) => total + (product.price * product.quantity), 0)
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 跳转到商品详情
const goToProduct = (productId) => {
  router.push(`/product-detail/${productId}`)
}

// 支付订单
const payOrder = () => {
  showToast('跳转到支付页面')
}

// 确认收货
const confirmReceive = () => {
  showToast('确认收货成功')
  order.value.status = 'completed'
  order.value.completeTime = new Date().toLocaleString()
}

// 评价订单
const reviewOrder = () => {
  showToast('跳转到评价页面')
}

// 取消订单
const cancelOrder = () => {
  showToast('订单已取消')
  order.value.status = 'cancelled'
}

// 再次购买
const rebuyOrder = () => {
  showToast('已添加到购物车')
}

// 查看物流
const viewLogistics = () => {
  showToast('查看物流详情')
}

// 显示提示
const showToast = (message) => {
  console.log(message)
}

onMounted(() => {
  // 从路由参数获取订单ID
  const orderId = route.params.id
  console.log('订单ID:', orderId)
  
  // 这里可以根据订单ID加载订单数据
  // loadOrder(orderId)
})
</script>

<style scoped>
.header {
  background: white;
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #666;
}

.title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.placeholder {
  width: 24px;
}

.status-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 32px 20px;
  color: white;
  margin-bottom: 12px;
}

.status-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.status-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid rgba(255, 255, 255, 0.3);
}

.status-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.status-details h3 {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.status-details p {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

.address-section,
.products-section,
.order-info-section,
.cost-section,
.logistics-section {
  background: white;
  margin-bottom: 12px;
  padding: 20px 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
}

.address-info {
  padding: 16px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  background: #f8f8f8;
}

.contact-info {
  display: flex;
  gap: 16px;
  margin-bottom: 8px;
}

.name {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.phone {
  font-size: 14px;
  color: #666;
}

.address-detail {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}

.product-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.product-item {
  display: flex;
  gap: 12px;
  padding: 16px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.product-item:hover {
  border-color: #ff4757;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.product-image {
  width: 80px;
  height: 80px;
  flex-shrink: 0;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.product-info {
  flex: 1;
}

.product-name {
  font-size: 14px;
  color: #333;
  margin: 0 0 8px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-spec {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
}

.product-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 16px;
  color: #ff4757;
  font-weight: 600;
}

.quantity {
  font-size: 12px;
  color: #999;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  font-size: 14px;
  color: #666;
}

.info-value {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.cost-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cost-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.cost-item.total {
  border-top: 1px solid #f0f0f0;
  padding-top: 16px;
  margin-top: 8px;
}

.cost-label {
  font-size: 14px;
  color: #333;
}

.cost-value {
  font-size: 14px;
  color: #333;
}

.cost-item.total .cost-label,
.cost-item.total .cost-value {
  font-size: 16px;
  font-weight: 600;
  color: #ff4757;
}

.logistics-info {
  padding: 16px;
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  background: #f8f8f8;
}

.logistics-company,
.logistics-number {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.label {
  font-size: 14px;
  color: #666;
}

.value {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.logistics-timeline {
  position: relative;
}

.logistics-timeline::before {
  content: '';
  position: absolute;
  left: 8px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #e0e0e0;
}

.timeline-item {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
  position: relative;
}

.timeline-item:last-child {
  margin-bottom: 0;
}

.timeline-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #e0e0e0;
  border: 3px solid white;
  flex-shrink: 0;
  z-index: 1;
}

.timeline-item.active .timeline-dot {
  background: #ff4757;
}

.timeline-content {
  flex: 1;
}

.timeline-time {
  font-size: 12px;
  color: #999;
  margin-bottom: 4px;
}

.timeline-desc {
  font-size: 14px;
  color: #333;
  line-height: 1.4;
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 16px;
  border-top: 1px solid #f0f0f0;
  z-index: 100;
}

.action-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.action-buttons .nut-button {
  flex: 1;
  height: 48px;
  font-size: 14px;
  font-weight: 600;
}
</style>
